package com.k2data.k2app.service;

import com.k2data.k2app.domain.po.JobTaskDO;
import com.k2data.k2app.mapper.JobTaskMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author clb
 */
@Log4j2
@Service
public class JobTaskService {
    @Autowired
    private JobTaskMapper jobTaskMapper;

    @Transactional
    public Integer insert(JobTaskDO jobTaskDO){
        return jobTaskMapper.insert(jobTaskDO);
    }

    @Transactional
    public JobTaskDO getByJobId(Long jobId){
        JobTaskDO jobTaskDO = new JobTaskDO();
        jobTaskDO.setJobId(jobId);
        List<JobTaskDO> list = jobTaskMapper.getByCondition(jobTaskDO);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    public List<JobTaskDO> getByTaskId(Long taskId){
        JobTaskDO jobTaskDO = new JobTaskDO();
        jobTaskDO.setTaskId(taskId);
        List<JobTaskDO> list = jobTaskMapper.getByCondition(jobTaskDO);
        return list;
    }
    public Integer selectCount(JobTaskDO jobTaskDO){
        int i = jobTaskMapper.selectCount(jobTaskDO);
        return i;
    }

    public List<JobTaskDO> queryList(JobTaskDO jobTaskDO){
        List<JobTaskDO> list = jobTaskMapper.queryList(jobTaskDO);
        return list;
    }

    public List<JobTaskDO> getByModel(Long projectId){
        JobTaskDO jobTaskDO = new JobTaskDO();
        jobTaskDO.setProjectId(projectId);
        List<JobTaskDO> list = jobTaskMapper.getByCondition(jobTaskDO);
        return list;
    }

    @Transactional
    public Integer updateStatusByJob(JobTaskDO jobTaskDO){
        return jobTaskMapper.updateStatusByJob(jobTaskDO);
    }

    public Integer deleteByJobId(Long jobId){
        JobTaskDO jobTaskDO = new JobTaskDO();
        jobTaskDO.setJobId(jobId);
        return jobTaskMapper.deleteByCondition(jobTaskDO);
    }

    public Integer deleteByTaskId(Long taskId){
        JobTaskDO jobTaskDO = new JobTaskDO();
        jobTaskDO.setTaskId(taskId);
        return jobTaskMapper.deleteByCondition(jobTaskDO);
    }

    public Integer deleteByProjectId(Long projectId){
        JobTaskDO jobTaskDO = new JobTaskDO();
        jobTaskDO.setProjectId(projectId);
        return jobTaskMapper.deleteByCondition(jobTaskDO);
    }

    /**
     * 最新成功的作业
     * @param taskId
     * @return
     */
    public JobTaskDO getLatestByTaskId(Long taskId){
        JobTaskDO jobTaskDO = new JobTaskDO();
        jobTaskDO.setTaskId(taskId);
        JobTaskDO jobTaskDO1 = jobTaskMapper.getLatestByTaskId(jobTaskDO);
        return jobTaskDO1;
    }
}
